﻿
Ext.define('app.system.maildefined.Log', {
    extend: 'Ext.panel.Panel',
    border: false,
    layout: 'border',
    treeView: false, // 树形视图
    initComponent: function () {
        this.items = [this.getInQuiryPanel(), this.getFilterPanel()];
        this.callParent();
    },
    getInQuiryPanel: function () //任务列表
    {
        if (!this.InQuiryPanel) {
            this.InQuiryPanel = new Ext.Panel(
           {
               //title: '任务中心',
               margins: '5',
               region: 'center',

               layout: 'border',
               items: [
                  this.getCardPanel()
               ]
           });
        }

        return this.InQuiryPanel;
    },

    getFilterPanel: function () {
        var _this = this;
        if (!this.filterPanel) {
            this.filterPanel = new Ext.Panel(
               {
                   margins: '5 5 5 0',
                   region: 'east',
                   autoScroll: true,
                   //split: true,
                   width: 300,
                   tbar: [
                       "<b>筛选邮件日志</b>",
                       '->',
                       {
                           text: '清除筛选',
                           scope: this,
                           handler: function () {
                               console.log('清除筛选');
                               this.clearFilter();
                           }
                       }
                   ],
                   defaults: {
                       border: false,
                       frame: false
                   },
                   items: [this.getFilterForm()
                   ]
               });
        }
        console.log('xx');
        return this.filterPanel;

    },

    getFilterForm: function () {
        var mainpanel = this;
        if (!this.formPanel) {
            this.formPanel = new Ext.form.FormPanel(
           {
               labelWidth: 10,
               labelAlign: 'left',
               bodyStyle: 'padding:5px',
               defaultType: 'textfield',
               defaults: {
                   width: 250
               },
               layoutConfig: {
                   columns: 2
               },
               items: [
                    {
                        inputValue: '',
                        emptyText: '主题',
                        name: 'Subject'
                    },
                    {
                        name: 'IsError',
                        xtype: 'combo',
                        mode: 'local',
                        editable: false,
                        store: new Ext.data.ArrayStore({
                            fields: [
                                'myId',
                                'displayText'
                            ],
                            data: [['2', '所有'], ['-1', '失败'], ['1', '成功']]
                        }),
                        valueField: 'myId',
                        displayField: 'displayText'
                    },
                   {
                       xtype: 'datefield',
                       format: 'Y-m-d',
                       emptyText: '起始日',
                       name: 'StartDate'
                   },
                   {
                       xtype: 'datefield',
                       format: 'Y-m-d',
                       emptyText: '截止日',
                       name: 'EndDate'
                   },
                   {
                       xtype: 'panel',
                       border: false,
                       width: '100%',
                       layout: 'hbox',
                       layoutConfig: { padding: '5', pack: 'end', align: 'middle' },
                       items: [
                           {
                               xtype: 'button',
                               text: '查询',
                               width: 60,
                               handler: function () { mainpanel._refreshStore(); }
                           }
                       ]
                   }
               ]
           });
        }

        return this.formPanel;

    },

    _refreshStore: function () {
        this.getTaskStore().reload();
    },

    clearFilter: function () {
        this.getFilterForm().form.reset();
        this.getTaskStore().clearFilter();
    },
    getTaskStore: function () {
        if (!this.taskStore) {

            var main = this;
            this.taskStore = new Ext.data.JsonStore({
                autoLoad: true,
                pageSize: 30,
                fields: ['id', 'body', 'createOn', 'errorMsg', 'isSuccess', 'mainTypes', 'receiver',
                    'subject', 'isSend'],
                proxy: {
                    type: 'ajax',
                    url: '/api/services/app/project/GetMailLog',
                    reader: {
                        type: 'json',
                        rootProperty: 'result.items',
                        totalProperty: 'result.totalCount'
                    }
                },
                listeners: {
                    beforeload: function (store, opt, evt) {
                        var form = main.getFilterForm().getForm();
                        var formValues = form.getFieldValues();
                        for (var param in formValues) {
                            if (form.findField(param) && form.findField(param).emptyText == formValues[param]) {
                                formValues[param] = undefined;
                            }
                        }

                        var baseParams = {
                            Subject: formValues["Subject"]
                          , EndDate: formValues["EndDate"]
                          , StartDate: formValues["StartDate"]
                          , IsError: formValues["IsError"]
                        };
                        store.proxy.extraParams = baseParams;
                    }
                }
            });
        }
        return this.taskStore;
    },
    getCardPanel: function () {
        var me = this;
        if (!this.cardPanel) {
            var cellEditing = Ext.create('Ext.grid.plugin.CellEditing', {
                clicksToEdit: 1
            });
            this.cardPanel =
                    new Ext.grid.GridPanel(
                {
                    border: false,
                    region: 'center',
                    tbar: [
                        {
                            text: '重新发送', glyph: 0xf1d8, handler: function () {
                                me.sendMail();
                            }
                        }, '->',
                        {
                            text: '保存', xtype: 'button', glyph: 0xf0c7, handler: function () {
                                me.saveEdit();
                            }
                        }
                    ],
                    loadMask: { msg: 'Loading ' },
                    dockedItems: [{
                        xtype: 'pagingtoolbar',
                        dock: 'bottom',
                        store: this.getTaskStore(),
                        displayInfo: true
                    }],
                    plugins: [cellEditing],
                    columns:
                    [
                    new Ext.grid.RowNumberer({ width: 28 }),
                        { header: "主题", sortable: true, minWidth: 300, dataIndex: 'subject', align: 'center' },
                        { header: "收件人", sortable: true, dataIndex: 'receiver', align: 'center', editor: {} },
                        { header: "是否成功", sortable: true, dataIndex: 'isSuccess', align: 'center' },
                        { header: "失败描述", sortable: true, dataIndex: 'errorMsg', align: 'center' },
                        { header: "邮件内容", sortable: true, dataIndex: 'body', align: 'center' },
                        { header: "创建时间", sortable: true, dataIndex: 'createOn', align: 'center' }
                    ],
                    store: this.getTaskStore()
                });
        }
        return this.cardPanel;
    },
    saveEdit: function () {
        var me = this;
        var arry = [];
        var updates = me.getTaskStore().getUpdatedRecords();
        for (var i = 0; i < updates.length; i++) {
            var record = updates[i];
            arry.push({ key: record.data.id, value: record.data.receiver });
        }
        if (arry.length == 0) {
            Ext.Msg.alert('系统提示', "没有需要保存的数据");
            return false;
        }
        App.AjaxPost('/api/services/app/mailDefined/SetMailReceiver', arry, function (data) {
            me.getTaskStore().load();
        });
    },
    sendMail: function () {
        var me = this;
        var sel = me.getCardPanel().getSelection()[0];
        if (!sel) {
            Ext.Msg.alert('系统提示', "请选择发送记录");
            return false;
        }
        App.AjaxGet('/api/services/app/project/GetMailRecord?recordId=' + sel.data.id, function (data) {
            me.getTaskStore().load();
        });
    }
});